<script setup lang="ts">

import {message} from "ant-design-vue";
import {WxtBizCode, WxtUtils} from "~/contentScripts/views/wxt/lib/wxtUtils";
import WxtModal from "~/contentScripts/views/wxt/wxt-modal.vue";

const title = '批量调整出价';
const props = defineProps({
  keywords: {
    type: Array,
    default: () => [],
  },
  bizCode: {
    type: String as PropType<WxtBizCode>,
    default: WxtBizCode.关键字推广,
  },
});
// 调整出价类型 自主出价 提高出价 降低出价 提高出价百分比 降低出价百分比
const adjustType = ref('自主出价');
const adjustPrice = ref<number | undefined>(undefined);
const show = ref(false);
const showModal = () => {
  if (props.keywords.length === 0) {
    message.error('请选择需要批量处理的数据！');
    return;
  }
  show.value = true;
}
const calcBidPrice = (price: number) => {
  const type = adjustType.value;
  if (type === '自主出价') {
    return adjustPrice.value;
  } else if (type === '提高出价') {
    return price + adjustPrice.value;
  } else if (type === '降低出价') {
    return price - adjustPrice.value;
  } else if (type === '提高出价百分比') {
    return price * (1 + (adjustPrice.value / 100));
  } else if (type === '降低出价百分比') {
    return price * (1 - (adjustPrice.value / 100));
  }
  return price;
}
const emit = defineEmits(['refresh']);
const handle = () => {
  if (adjustPrice.value === undefined) {
    message.error('请输入调整出价!');
    return;
  }
  show.value = false;
  const wordPackageList: any[] = [];
  const bidwordList: any[] = [];
  props.keywords.map((c: any) => {
    console.log(c);
    if (c.type == 'kw') {
      bidwordList.push({
        adgroupId: c.adgroupId,
        campaignId: c.campaignId,
        bidPrice: calcBidPrice(c.bidPrice),
        bidStrategyInfo: null,
        bidwordId: c.bidwordId,
      });
    } else if (c.wordPackageId>0) {
      wordPackageList.push({
        adgroupId: c.adgroupId,
        campaignId: c.campaignId,
        bidPrice: calcBidPrice(c.bidPrice),
        wordPackageId: c.wordPackageId,
        wordPackageType: c.wordPackageType,
        status: c.status,
      });
    }
  });
  if (!wordPackageList.length && !bidwordList.length) {
    message.error('没有需要调整出价的数据!');
    return;
  }
  if (wordPackageList.length > 0) {
    WxtUtils.customRequest('https://one.alimama.com/wordpackage/update.json', props.bizCode, {
      wordPackageList: wordPackageList,
    }).then(() => {
      message.success('调价成功!');
      emit('refresh');
      show.value = false;
    });
  }
  if (bidwordList.length > 0) {
    WxtUtils.customRequest('https://one.alimama.com/bidword/update.json', props.bizCode, {
      bidwordList: bidwordList,
    }).then(() => {
      message.success('调价成功!');
      if (wordPackageList.length === 0) {
        emit('refresh');
        show.value = false;
      }
    });
  }
}
</script>

<template>
  <div @click="showModal" style="display: inline-block">
    <slot>
      <a-button type="primary" ghost size="small">批量调整出价</a-button>
    </slot>
  </div>
  <wxt-modal :title="title" v-model:visible="show" width="500px" :style="{
    top:'revert-layer',
  }"
  >
    <!--  // 调整出价类型 自主出价 提高出价 降低出价 提高出价百分比 降低出价百分比-->
    <div style="text-align: center">
      <a-input-number v-model:value="adjustPrice" min="0" max="999999" placeholder="请输入" step="0.01">
        <template #addonBefore>
          <a-select v-model:value="adjustType" style="width: 120px;">
            <a-select-option value="自主出价">自主出价</a-select-option>
            <a-select-option value="提高出价">提高出价</a-select-option>
            <a-select-option value="降低出价">降低出价</a-select-option>
            <a-select-option value="提高出价百分比">提高出价%</a-select-option>
            <a-select-option value="降低出价百分比">降低出价%</a-select-option>
          </a-select>
        </template>
        <template #addonAfter>
          {{ adjustType.indexOf('百分比') >= 0 ? '%' : '元' }}
        </template>
      </a-input-number>
    </div>
    <a-divider/>
    <a-row>
      <a-col flex="1">

      </a-col>
      <a-col>
        <a-space>
          <a-button @click="show=false">取消</a-button>
          <a-button type="primary" @click="handle">确定</a-button>
        </a-space>
      </a-col>
    </a-row>
  </wxt-modal>
</template>

<style scoped>

</style>
